package com.wd.util;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class InputStreamUtil {

	private static Log log = LogFactory.getLog(InputStreamUtil.class);

	public static byte[] inputStream2BytesArray(InputStream input) {
		byte[] byt = new byte[1024];
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		int size = 0;
		try {
			size = input.read(byt, 0, 1024);
			while (size != -1) {
				baos.write(byt,0,size);
				baos.flush();
				size = input.read(byt, 0, 1024);
			}
			byt = baos.toByteArray();
		} catch (IOException ioe) {
			log.error("", ioe);
		} finally {
			if (baos != null) {
				try {
					baos.close();
				} catch (IOException e) {
					log.error("", e);
				}
			}
		}
		return byt;
	}
	
	public static void main(String []agrs) {
		FileInputStream in = null;
			long startTime = System.currentTimeMillis();
			try {
				in = new FileInputStream(new File("c:/test/UltraEdit_14.20.0.1033_SC.exe"));
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
			//定义字节大小
			byte [] buf = InputStreamUtil.inputStream2BytesArray(in);
			System.out.println("字节大小-->"+buf.length);
			try {
				if(in != null){
					in.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			long endTime = System.currentTimeMillis();
			System.out.println("文件复制时间-->" + (endTime-startTime)+"毫秒!");
	}
}
